Imports Microsoft.Win32

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If chksend.Checked = True Then
            Timer1.Interval = 5000
        Else : Timer1.Interval = 5
        End If
        lst1.Items.Clear()
        Timer1.Enabled = True
        Button2.Enabled = False
        Button3.Enabled = True
        'replace.yesdigit()
        'If Len(lbl1.Text) = Len(txt1.Text) Then
        '    MsgBox("congradulations")
        'End If

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        On Error GoTo 150
        'Randomize()
        'MsgBox(Int(Rnd() * 10) + 1)
        My.Computer.Clipboard.SetText(lst1.SelectedItem)
150:    Exit Sub

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'lbl1.Text = ""
        st = ""
        Dim aaaa As Integer = 1
        For iiii = 1 To Val(txt2.Text)
            aaaa = 1
            replace.lst.Clear()
            'lbl1.text = ""
            replace.yesdigit()
            'Dim a As String = txt1.Text
            'a.Replace("#", replace.notdigit)
            For i = 1 To Len(txt1.Text)
                a = Mid(txt1.Text, i, 1)
                If a = "#" Then
                    'MsgBox(replace.notdigit)
                    st = st & replace.notdigit
                ElseIf a = "?" Then
                    'MsgBox("WildCard " & replace.wildcard)
                    st = st & replace.wildcard
                ElseIf a = "^" Then
                    'MsgBox("letter " & replace.letter)
                    st = st & replace.letter
                ElseIf a = "%" Then
                    bqr = replace.lst.Count
                    ss = aaaa - 1
                    st = st & replace.lst.Item(ss)
                    aaaa = aaaa + 1
                Else : st = st & a
                End If
                'here
            Next
            If Caps.Checked = True Then
                st = UCase(st)
            Else : st = LCase(st)
            End If

            If chksend.Checked = True Then
                If txtpre.Text = "" Then
                Else : SendKeys.Send(txtpre.Text)
                    SendKeys.Send("{Enter}")
                End If
                SendKeys.Send(st)
                SendKeys.Send("{enter}")
                SendKeys.Send(txtcompany.Text)
                If iiii = Val(txt2.Text) Then
                Else : SendKeys.Send("{tab}")
                End If
                lst1.Items.Add(st)
            Else : lst1.Items.Add(st)
            End If
            st = ""
        Next
        Timer1.Enabled = False
        Button2.Enabled = True
    End Sub

    Private Sub Button3_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim path As String = My.Application.Info.DirectoryPath & "\Info\PCS.bts"
        Try
            UpdateSaved() 'update the list of saved templates
        Catch ex As Exception
            MsgBox("Error:" & ex.Message, MsgBoxStyle.Critical, "Error. . .")
        End Try
    End Sub

    ''' <summary>
    ''' Updates the list of previously saved product key templates
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub UpdateSaved()
        Existing.Items.Clear()
        Dim path As String = My.Application.Info.DirectoryPath & "\Info\PCS.bts"
        If My.Computer.FileSystem.FileExists(path) = False Then
            'Dim bts1() As Byte = System.Text.UnicodeEncoding.UTF32.GetBytes("")
            My.Computer.FileSystem.CreateDirectory(My.Application.Info.DirectoryPath & "\Info\")
            My.Computer.FileSystem.WriteAllText(path, " ;", False)
        End If
        Dim s As String = My.Computer.FileSystem.ReadAllText(path)
        's = System.Text.UnicodeEncoding.UTF32.GetChars(bts)
        If s = "" Or s = " " Then
            s = " ;"
        End If
        Do While Len(s) > 1
            position = InStr(s, ";")
            Value = Mid(s, 1, position - 1)
            If Value = " " Then
                GoTo 50
            End If
            Existing.Items.Add(Value)
50:         s = Mid(s, position + 1)
        Loop
        Dim graph As Graphics = Me.CreateGraphics
        Dim size As New SizeF
        Dim width As Integer = 0
        For Each item As String In Existing.Items
            size = graph.MeasureString(item, Existing.Font)
            If size.Width > width Then
                width = size.Width
            End If
        Next
        If width = 0 Then
            width = Existing.Width
        End If
        Existing.DropDownWidth = width
    End Sub

    Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        a = txtpre.Text
        b = txtcompany.Text
        txtpre.Text = b
        txtcompany.Text = a
    End Sub

    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs)
        UpdateSaved()
    End Sub

    Private Sub LinkLabel3_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel3.LinkClicked
        Try
            Dim path As String = My.Application.Info.DirectoryPath & "\Info\PCS.bts"
            My.Computer.FileSystem.DeleteFile(path, FileIO.UIOption.OnlyErrorDialogs, FileIO.RecycleOption.DeletePermanently)
            My.Computer.FileSystem.WriteAllText(path, " ;", False)
        Catch ex As Exception

            MsgBox("Error:" & ex.Message, MsgBoxStyle.Critical, "Error. . .")
        End Try
        UpdateSaved()
    End Sub

    Private Sub Existing_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Existing.SelectedIndexChanged
        If Existing.Items.Count = 0 Or Existing.SelectedItem = "" Then
            Exit Sub
        End If
        Dim s As String = Existing.SelectedItem
        pos = InStr(s, "(")
        value = Mid(s, 1, pos - 1)
        txt1.Text = value
    End Sub

    Private Sub LinkLabel2_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel2.LinkClicked
        If txt1.Text = "" Then
            Exit Sub
        End If

        Dim dir As String = My.Application.Info.DirectoryPath
        Dim exists As Boolean
        exists = My.Computer.FileSystem.DirectoryExists(dir & "\Info\")
        'Dim bts() As Byte
        Dim s As String
        If exists = True Then
            Dim Name As String = InputBox("Please enter a name for this product key Template", "Product Key Template", "")

            'bts = System.Text.UnicodeEncoding.UTF32.GetBytes(txt1.Text & "(" & Name & ")" & ";")
            s = txt1.Text & "(" & Name & ");"
            My.Computer.FileSystem.WriteAllText(dir & "\Info\PCS.bts", s, True)
        ElseIf exists = False Then
            Dim Name As String = InputBox("Please enter a name for this product key Template", "Product Key Template", "")
            My.Computer.FileSystem.CreateDirectory(dir)
            'bts = System.Text.UnicodeEncoding.UTF32.GetBytes(txt1.Text & "(" & Name & ")" & ";")
            s = txt1.Text & "(" & Name & ");"
            My.Computer.FileSystem.WriteAllText(dir & "\Info\PCS.bts", s, True)

        End If
        UpdateSaved()
    End Sub

    Private Sub Button3_Click_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim s As String = ""
        Dim Delimiter As String = My.Settings.Delimiter
        For Each item As String In lst1.Items
            s = s & item & Delimiter
        Next
        Dim Result As DialogResult = Save1.ShowDialog()
        Dim AppendResult As MsgBoxResult = MsgBox("Do you want to append text?" & Chr(10) & "YES = Append Text" & Chr(10) & "NO = Overwrite existing text", MsgBoxStyle.YesNo, "Append?")
        Dim Append As Boolean = True
        If AppendResult = MsgBoxResult.Yes Then
            Append = True
        Else : Append = False
        End If

        If Result = Windows.Forms.DialogResult.OK Then
            My.Computer.FileSystem.WriteAllText(Save1.FileName, s, Append)
        End If

        '**Comment out the following to stop from asking to display the text file**
        Dim ShowResult As MsgBoxResult = MsgBox("Do you want to display the saved text file?", MsgBoxStyle.YesNo, "Product Keys")
        If ShowResult = MsgBoxResult.Yes Then
            Process.Start(Save1.FileName)
        End If


    End Sub
End Class
